package cn.ticket.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import cn.ticket.model.Constants;


@Service
public class ExcelExportService {
	public void produceRefundLog(String title, String[] headers, List<Map<String, Object>> dataset,
			XSSFWorkbook workbook) {
		// 生成一个表格
		XSSFSheet sheet = workbook.createSheet(title);
		// 设置表格默认列宽度为15个字节
		sheet.setDefaultColumnWidth(15);
		// 生成一个样式
		XSSFCellStyle style = workbook.createCellStyle();
		// 设置这些样式
		style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
		style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		// 生成一个字体
		XSSFFont font = workbook.createFont();
		font.setColor(HSSFColor.VIOLET.index);
		font.setFontHeightInPoints((short) 12);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		// 把字体应用到当前的样式
		style.setFont(font);

		XSSFCellStyle style2 = workbook.createCellStyle();
		style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
		style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style2.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER);
	//	style2.setAlignment(XSSFCellStyle.ALIGN_LEFT);
		
		XSSFCellStyle style3 = workbook.createCellStyle();
		style3.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
		style3.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		style3.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style3.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style3.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style3.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style3.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER);
		style3.setAlignment(XSSFCellStyle.ALIGN_LEFT);
		// 生成另一个字体
		XSSFFont font2 = workbook.createFont();
		font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
		// 把字体应用到当前的样式
		style2.setFont(font2);
		
		// 产生表格标题行
		XSSFRow row = sheet.createRow(0);
		for (int i = 0; i < headers.length; i++) {
			XSSFCell cell = row.createCell(i);
			cell.setCellStyle(style);
			XSSFRichTextString text = new XSSFRichTextString(headers[i]);
			cell.setCellValue(text);
		}
		for(int j=0;j<dataset.size();j++){
			XSSFRow row1 = sheet.createRow(j+1);
			Map<String, Object> refundlog=dataset.get(j);
					
				XSSFCell cell1 = row1.createCell(0);				
				cell1.setCellStyle(style2);
				cell1.setCellValue((String)refundlog.get("name"));
				
				XSSFCell cell2 = row1.createCell(1);				
				cell2.setCellStyle(style2);
				cell2.setCellValue((String)refundlog.get("seatName"));
			
				XSSFCell cell3 = row1.createCell(2);				
				cell3.setCellStyle(style2);
				cell3.setCellValue((String)refundlog.get("routeName"));
				
				XSSFCell cell4 = row1.createCell(3);				
				cell4.setCellStyle(style2);			
				cell4.setCellValue((Integer)refundlog.get("busNumber")+"号车"+(Integer)refundlog.get("seatNumber")+"座");								
								

				XSSFCell cell5 = row1.createCell(4);				
				cell5.setCellStyle(style2);			
				cell5.setCellValue(((Date)refundlog.get("date")).toLocaleString());
				
				XSSFCell cell6 = row1.createCell(5);				
				cell6.setCellStyle(style3);			
				cell6.setCellValue((String)refundlog.get("reason"));										
		}
	}
	
	public void produceWorkTicketOrder(String title, String[] headers, List<Map<String, Object>> dataset,
			XSSFWorkbook workbook) {
		// 生成一个表格
		XSSFSheet sheet = workbook.createSheet(title);
		// 设置表格默认列宽度为15个字节
		sheet.setDefaultColumnWidth(15);
		// 生成一个样式
		XSSFCellStyle style = workbook.createCellStyle();
		// 设置这些样式
		style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);
		style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style.setAlignment(HSSFCellStyle.ALIGN_LEFT);
		// 生成一个字体
		XSSFFont font = workbook.createFont();
		font.setColor(HSSFColor.VIOLET.index);
		font.setFontHeightInPoints((short) 12);
		font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
		// 把字体应用到当前的样式
		style.setFont(font);

		XSSFCellStyle style2 = workbook.createCellStyle();
		style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
		style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style2.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER);
	//	style2.setAlignment(XSSFCellStyle.ALIGN_LEFT);
		
		XSSFCellStyle style3 = workbook.createCellStyle();
		style3.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
		style3.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
		style3.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		style3.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		style3.setBorderRight(HSSFCellStyle.BORDER_THIN);
		style3.setBorderTop(HSSFCellStyle.BORDER_THIN);
		style3.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		style3.setVerticalAlignment(HSSFCellStyle.ALIGN_CENTER);
		style3.setAlignment(XSSFCellStyle.ALIGN_LEFT);
		// 生成另一个字体
		XSSFFont font2 = workbook.createFont();
		font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
		// 把字体应用到当前的样式
		style2.setFont(font2);
		
		// 产生表格标题行
		XSSFRow row = sheet.createRow(0);
		for (int i = 0; i < headers.length; i++) {
			XSSFCell cell = row.createCell(i);
			cell.setCellStyle(style);
			XSSFRichTextString text = new XSSFRichTextString(headers[i]);
			cell.setCellValue(text);
		}
		for(int j=0;j<dataset.size();j++){
			XSSFRow row1 = sheet.createRow(j+1);
			Map<String, Object> refundlog=dataset.get(j);
					
				XSSFCell cell1 = row1.createCell(0);				
				cell1.setCellStyle(style2);
				cell1.setCellValue((String)refundlog.get("seatName"));
				
				XSSFCell cell2 = row1.createCell(1);				
				cell2.setCellStyle(style2);
				cell2.setCellValue((String)refundlog.get("departmentName"));
			
				XSSFCell cell3 = row1.createCell(2);				
				cell3.setCellStyle(style2);
				cell3.setCellValue((int)refundlog.get("seatNumber"));
				
				XSSFCell cell4 = row1.createCell(3);				
				cell4.setCellStyle(style2);
				cell4.setCellValue(new SimpleDateFormat(Constants.DATEFORMAT).format((Date)refundlog.get("date")));
				
//				XSSFCell cell4 = row1.createCell(3);				
//				cell4.setCellStyle(style2);			
//				cell4.setCellValue((Integer)refundlog.get("busNumber")+"号车"+(Integer)refundlog.get("seatNumber")+"座");								
//								

				XSSFCell cell5 = row1.createCell(4);				
				cell5.setCellStyle(style2);			
				cell5.setCellValue((String)refundlog.get("routeName"));
				
//				XSSFCell cell6 = row1.createCell(5);				
//				cell6.setCellStyle(style3);			
//				cell6.setCellValue((String)refundlog.get("reason"));										
		}
	}
}
